module.exports = {
  format: function (coupons) {
    let list = [], foreshow = [], curUnix = wx.time.curUnix();
    if (!coupons || coupons.length < 1) return list;
    for (let item of coupons) {
      // 优先显示当前可领，超期不显
      if (item.term_of_validity_type == 1 || curUnix < item.end_time) {
        /*// 数量格式化
        item.count = Number(item.count);
        item.get_count = Number(item.get_count);
        // 金额格式化
        item.bigMoney = false;
        if (item.money) {
          if (item.money > 100) item.bigMoney = true;
          let [int, float] = item.money.split('.');
          if (float == '00') {
            item.money = int;
            if (item.money > 1000) item.bigMoney = true;
          }
        }
        if (item.at_least) {
          let [int, float] = item.at_least.split('.');
          if (float == '00') item.at_least = int;
        }
        if (item.type == 2) item.discount /= 10;
        if (item.at_least / 100000 > 1) item.at_least = (item.at_least / 10000).toFixed(2) + '万';
        // 领取期限
        item.get_start_time = Number(item.get_start_time);
        item.get_end_time = Number(item.get_end_time);
        item.getStart = wx.time.toDate(item.get_start_time, 'Y-M-D');
        item.getEnd = wx.time.toDate(item.get_end_time, 'Y-M-D');
        // 使用期限
        item.start_time = wx.time.toDate(item.start_time, 'Y-M-D');
        item.end_time = wx.time.toDate(item.end_time, 'Y-M-D');*/
        item = this.convert(item);
        // 归类
        if (curUnix < item.get_start_time) {
          item.canReceive = false;
          foreshow.push(item);
        } else if (curUnix < item.get_end_time) {
          item.canReceive = true;
          list.push(item);
        }
      }
    }
    return list.concat(foreshow);
  },
  convert: function (coupon) {
    // 数量格式化
    coupon.count = Number(coupon.count);
    coupon.get_count = Number(coupon.get_count);
    // 金额格式化
    coupon.bigMoney = false;
    if (coupon.money) {
      if (coupon.money >= 100) coupon.bigMoney = true;
      let [int, float] = coupon.money.split('.');
      if (float == '00') {
        coupon.money = int;
        if (coupon.money < 1000) coupon.bigMoney = false;
      }
    }
    if (coupon.at_least) {
      let [int, float] = coupon.at_least.split('.');
      if (float == '00') coupon.at_least = int;
    }
    if (coupon.type == 2) coupon.discount /= 10;
    if (coupon.at_least / 100000 > 1) coupon.at_least = (coupon.at_least / 10000).toFixed(2) + '万';
    // 领取期限
    coupon.get_start_time = Number(coupon.get_start_time);
    coupon.get_end_time = Number(coupon.get_end_time);
    coupon.getStart = wx.time.toDate(coupon.get_start_time, 'Y-M-D');
    coupon.getEnd = wx.time.toDate(coupon.get_end_time, 'Y-M-D');
    // 使用期限
    coupon.start_time = wx.time.toDate(coupon.start_time, 'Y-M-D');
    coupon.end_time = wx.time.toDate(coupon.end_time, 'Y-M-D');

    return coupon;
  },
  receive: function (couponId) {
    if (!wx.getStorageSync('rf_token')) {
      wx.util.toast('请先登录');
      return;
    }
    wx.util.request('marketing.coupon.new', {
      body: {id: couponId}
    }).then(() => {
      wx.util.toast('已领取');
    }).catch(err => {
      console.log(err);
    });
  }
}